package com.linghu.Service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.app.ApplicationUsage;
import com.alibaba.dashscope.app.RagApplicationParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import com.linghu.Service.QueryApiService;
import com.linghu.entity.Ans;
import org.springframework.stereotype.Service;

/**
 * @author linghu
 * @date 2024/4/29 15:23
 */
@Service
public class QueryApiServiceImpl implements QueryApiService {
    /**
     * @Description: 传入问题答案返回响应
     * @param ans
     * @return: java.lang.String
     * @Author: codelinghu
     * @Date: 2024/4/29
     */
    @Override
    public String usage(Ans ans) throws NoApiKeyException, InputRequiredException {
        Constants.apiKey="sk-W9yPJw0c7Q";
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg = Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        //传入ans
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(ans.getAns()).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);

        GenerationParam param = GenerationParam.builder()
                .model("chatglm3-6b")
                .messages(msgManager.get())
                .build();
        Generation gen = new Generation();
        GenerationResult result = gen.call(param);
        //输出文本答案
//        System.out.println("result="+result.getOutput().getText());
//        System.out.println(JsonUtils.toJson(result));//输出响应
        return JsonUtils.toJson(result.getOutput().getText().replace("\n",""));
    }

    @Override
    public ApplicationResult ragCall(Ans ans) throws NoApiKeyException, InputRequiredException {
        Constants.apiKey="sk-W9yPJw0c7Q";
        RagApplicationParam param = RagApplicationParam.builder()
                .appId("899c8e534eb64a40ab9aac9ab20f7414")
                .prompt(ans.getAns())
                .build();
        Application application = new Application();
        ApplicationResult result = application.call(param);

        System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
                result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());

        if (result.getUsage() != null && result.getUsage().getModels() != null) {
            for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
                System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
                        usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
            }
        }
        return result;
    }
}
